package com.cnhis.cloudhealth.clinical.clidoctor.medicine.bo;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cnhis.cloudhealth.clinical.clidoctor.cliparamter.bo.CliParamterBo;
import com.cnhis.cloudhealth.clinical.clidoctor.medicine.dao.MedicineDao;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.utils.DateUtils;
import java.util.HashMap;
import com.cnhis.cloudhealth.commons.utils.Page;
import com.cnhis.cloudhealth.commons.utils.StaticKeys;

@Service
public class MedicineBo {
	@Resource
	private MedicineDao medicineDao;
	@Resource
	private CliParamterBo cliParamterBo;
	@SuppressWarnings("unchecked")
	public ModelVo queryMedicine(Map map,int currPage,int pageSize)throws Exception{
		ModelVo mo = new ModelVo();
		Page pg = null;
		try {
			String value = cliParamterBo.getSysValue(map);
			map.put("para63a", value);
			pg =medicineDao.queryMedicine(map, currPage, pageSize);
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		
		mo.setList(pg.getList());
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setTotal(pg.getTotalNumber());
		mo.setRows(pg.getList());
		return mo;
	}
	public ModelVo  patientAllergyMedicine(Map map)throws Exception{
		ModelVo modelVo = new ModelVo();
		List list =null;
		try {
			list=medicineDao.patientAllergyMedicine(map);
		} catch (Exception e) {
			modelVo.setResult(StaticKeys.OH_FALSE);
			return modelVo;
		}
	   
	   modelVo.setList(list);
	   modelVo.setRows(list);
	   modelVo.setResult(StaticKeys.OH_SUCCESS);
	   return modelVo;
	}
	public ModelVo medicineAllergy(Map map,int currPage,int pageSize)throws Exception{
		ModelVo mo = new ModelVo();
		Page pg = null;
		try {
			
			pg =medicineDao.medicineAllergy(map, currPage, pageSize);
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		
		mo.setList(pg.getList());
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setTotal(pg.getTotalNumber());
		mo.setRows(pg.getList());
		return mo;
	}
	public ModelVo medicineDefaultDosage(Map map)throws Exception{
		ModelVo modelVo = new ModelVo();
		List list =null;
		try {
			list=medicineDao.medicineDefaultDosage(map);
		} catch (Exception e) {
			modelVo.setResult(StaticKeys.OH_FALSE);
			return modelVo;
		}
	   
	   modelVo.setList(list);
	   modelVo.setRows(list);
	   modelVo.setResult(StaticKeys.OH_SUCCESS);
	   return modelVo;
	}
	public ModelVo executeTimeCase(Map map)throws Exception{
		ModelVo modelVo = new ModelVo();
		List list =null;
		try {
			list=medicineDao.executeTimeCase(map);
		} catch (Exception e) {
			modelVo.setResult(StaticKeys.OH_FALSE);
			return modelVo;
		}
	   
	   modelVo.setList(list);
	   modelVo.setRows(list);
	   modelVo.setResult(StaticKeys.OH_SUCCESS);
	   
	   return modelVo;
	}
	private String getParam(Integer productId,Integer programId,Integer paramNo )throws Exception{
		HashMap map = new HashMap();
		map.put("productId", productId);
		map.put("programId", programId);
		map.put("paramNo",paramNo);
		return medicineDao.getParam(map);
	}
	private Page selectParamno(Map map,int currPage,int pageSize)throws Exception{
		return medicineDao.selectParamno(map, currPage, pageSize);
	}
    private Page selectParam(Map map,int currPage,int pageSize)throws Exception{
		return medicineDao.selectParam(map, currPage, pageSize);
	}
	public ModelVo queryItemDetail(Map map,int currPage,int pageSize)throws Exception{
		Object lDate = map.get("lDate");
		if(lDate==null||"".equals(lDate.toString())){
			String date = DateUtils.getDateStr(new Date());
			map.put("lDate", date+" 23:59:59.999");
		}else{
			map.put("lDate", lDate.toString()+" 23:59:59.999");
		}
		String gparam60 = getParam(9999, 9999, 60);
		String aParamno=getParam(9999, 9999, 15);
		String aParam1 = getParam(9999, 9999, 1);
		map.put("gparam60", gparam60);
		map.put("aParamno", aParamno);
		map.put("aParam1", aParam1);
		
		
		
		ModelVo mo = new ModelVo();
		Page pg = null;
		try {
			if("0".equals(aParamno)){
				pg = selectParamno(map, currPage, pageSize);
			}else{
				List tmpBCKList = medicineDao.getTmpBCKList(map);
				map.put("tmpBCKList", tmpBCKList);
				pg = selectParam(map, currPage, pageSize);
			}
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		
		mo.setList(pg.getList());
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setTotal(pg.getTotalNumber());
		mo.setRows(pg.getList());
		return mo;
		
	}
	
	private String getBDN01(Map map)throws Exception{
		return medicineDao.getBDN01(map);
	}
	public ModelVo getDefaultPharmacy(Map map)throws Exception{
		ModelVo modelVo = new ModelVo();
		List list =null;
		try {
			String aParamno = getParam(9999, 9999, 15);
			if("0".equals(aParamno)){
				String BDN01 = getBDN01(map);
				map.put("BDN01", BDN01);
				list = medicineDao.getDefaultPharmacy0(map);
			}
			if("1".equals(aParamno)){
				list = medicineDao.getDefaultPharmacy1(map);
			}
			list=medicineDao.executeTimeCase(map);
		} catch (Exception e) {
			modelVo.setResult(StaticKeys.OH_FALSE);
			return modelVo;
		}
	   
	   modelVo.setList(list);
	   modelVo.setRows(list);
	   modelVo.setResult(StaticKeys.OH_SUCCESS);
	   
	   return modelVo;
	}
	public ModelVo getDrugCount(Map map)throws Exception{
		ModelVo modelVo = new ModelVo();
		List list =null;
		try {
			String aParamno=getParam(9999, 9999, 15);
			map.put("aParamno", aParamno);
			list=medicineDao.getDrugCount(map);
		} catch (Exception e) {
			modelVo.setResult(StaticKeys.OH_FALSE);
			return modelVo;
		}
	   
	   modelVo.setList(list);
	   modelVo.setRows(list);
	   modelVo.setResult(StaticKeys.OH_SUCCESS);
	   
	   return modelVo;
	}
	public ModelVo disabledDrug(Map map)throws Exception{
		ModelVo modelVo = new ModelVo();
		List list =null;
		try {
			Object adate = map.get("adate");
			if(adate==null||"".equals(adate.toString())){
				map.put("adate", DateUtils.getDateString(new Date()));
			}
			list=medicineDao.getDrugCount(map);
		} catch (Exception e) {
			modelVo.setResult(StaticKeys.OH_FALSE);
			return modelVo;
		}
	   
	   modelVo.setList(list);
	   modelVo.setRows(list);
	   modelVo.setResult(StaticKeys.OH_SUCCESS);
	   
	   return modelVo;
	}
}
